import paginationConf from './pagination';
import _ from 'lodash';
import { addNewMonitor, deleteMonitor, getMakerList, updateMonitor } from '@/services/monitorManage';

const MonitorModel = {
  namespace: 'monitorManage',
  state: {
    makers: [], //设备列表
    loading: false,
    selectedRowsKeys: [],
    modalVisible: false, //页面是否打开
    optiontype: null, //打开model操作类型
    makerDetail: {},
    effectPageInfo: {},
    drawerVisible: false,
    pagination: {
      ...paginationConf,
    },
  },
  effects: {
    *getData({ payload }, { call, put, select }) {
      const pagination = yield select((state) => state.monitorManage.pagination);
      const response = yield call(getMakerList, { ...payload, pagination });
      const makerList = _.get(response, 'data.items', []);

      yield put({
        type: 'updateDataSet',
        payload: {
          makerList,
          pagination: {
            total: _.get(response, 'data.total', null),
            pageNum: _.get(response, 'data.page', 1),
          },
        },
      });
    },
    *submitMonitorInfo({ payload, callback }, { call, put }) {
      const { type, formData } = payload;
      // 判断是更新还是新增
      const callFn = type === 'add' ? addNewMonitor : updateMonitor;
      const ret = yield call(callFn, formData);
      callback(ret);
    },
    *deleteMonitorInfo({ payload, callback }, { put, call }) {
      const ret = yield call(deleteMonitor, payload)
      callback(ret);
    },
    *updateMonitorInfo({ payload, callback }, { put, call }) {
      const ret = yield call(updateMonitor, payload)
      callback(ret);
    },
    changePage({ payload }, { put }) {
      put({
        type: 'changePage',
        payload,
      });
    },
  },
  reducers: {
    //改变Table表加载状态
    changeLoadingStatus(state, { payload }) {
      return { ...state, loading: !state.loading };
    },
    updateDataSet(state, { payload }) {
      return {
        ...state,
        makers: payload.makerList,
        pagination: { ...state.pagination, ...payload.pagination },
        loading: false,
        effectPageInfo: { current: state.pagination.start },
      };
    },
    //打开Model时调用
    changeModalVisibleAndSetValue(state, { payload }) {
      if (payload.type === 'add') {
        return {
          ...state,
          modalVisible: !state.modalVisible,
          makerDetail: {},
          optiontype: payload.type,
        };
      } else if (payload.type === 'update') {
        return {
          ...state,
          modalVisible: !state.modalVisible,
          optiontype: payload.type,
          makerDetail:
            {
              ...payload.data,
            } || {},
        };
      }
      return { ...state, modalVisible: !state.modalVisible };
    },

    //关闭Model时调用
    changeModelVisibleAndCloseModel(state) {
      return { ...state, modalVisible: !state.modalVisible, optiontype: null, makerDetail: {} };
    },

    //打开Drawer页面
    changeMonitorlevlDrawer(state, { payload }) {
      return { ...state, drawerVisible: !state.drawerVisible, makerDetail: payload };
    },
    changePage(state, { payload }) {
      return {
        ...state,
        pagination: { ...state.pagination, start: payload },
      };
    },
  },
};

export default MonitorModel;
